//
//  HRFunChatTopicCell.m
//  Hear
//
//  Created by mac on 2018/3/13.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRFunChatTopicCell.h"
#import "NSMutableAttributedString+GDAttString.h"
#import "HRDynamicCell.h"

@interface HRFunChatTopicCell (){
    EMMessageBodyType _messageType;
}
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, strong)UIButton *titleLabel;
@property(nonatomic, strong)HRSoundButton *soundButton;
@property(nonatomic, assign)CGFloat contentHeight;
@property(strong, nonatomic)EaseMessageModel *model;

@end
@implementation HRFunChatTopicCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier model:(id<IMessageModel>)model
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _messageType = model.bodyType;
        [self _setupSubviewsWithType:_messageType
                            isSender:model.isSender
                               model:model];
        
    }
    
    return self;
}
- (void)_setupSubviewsWithType:(EMMessageBodyType)messageType
                      isSender:(BOOL)isSender
                         model:(id<IMessageModel>)model{
    
    _model = model;
    NSString *contentStr = model.text;
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 8;
    NSDictionary *dic = @{NSFontAttributeName:CustomUIFont(14), NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    self.contentHeight = [contentStr boundingRectWithSize:CGSizeMake(kFullWidth-100, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.height;

    self.contentHeight +=40;
//    if (self.soundUrl.length){
//        contentHeight +=70;
//    }

   
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(adjuctFloat(50), 30,kScreenWidth-adjuctFloat(100), self.contentHeight-30)];
    backView.backgroundColor = CustomColor(240, 240, 240, 1);
    [self addSubview:(self.backView=backView)];
    backView.layer.cornerRadius = 8;
    backView.clipsToBounds = YES;



    UIButton *titleLabel = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, backView.width,backView.height)];
    titleLabel.enabled = NO;
    [titleLabel setContentEdgeInsets:UIEdgeInsetsMake(15, 15, 15, 15)];
    titleLabel.titleLabel.font = CustomUIFont(14);
    titleLabel.titleLabel.numberOfLines = 0;
    [titleLabel setTitleColor:FirstTextColor forState:0];
    [backView  addSubview:titleLabel];


    NSRange range = [contentStr rangeOfString:@"hearer君"];
    if (range.location != NSNotFound){
        [titleLabel setAttributedTitle:[NSMutableAttributedString arrtLabelText:contentStr arrFont:CustomUIFont(14) attColor:MainColor lineLengh:8 alignment:0 colorLenghFrom:range.location colorLengh:range.length] forState:0];
    }else{
        [titleLabel setAttributedTitle:[NSMutableAttributedString arrtLabelText:contentStr arrFont:CustomUIFont(14) attColor:FirstTextColor lineLengh:8 alignment:0 colorLenghFrom:0 colorLengh:contentStr.length] forState:0];
    }
    HRSoundButton *soundButton = [[HRSoundButton alloc] initWithFrame:CGRectMake(backView.width/4,titleLabel.bottom+10, backView.width/2, 50)];

    soundButton.leftImageView.frame = CGRectMake(15, 10, 13, 18);
    soundButton.voiceImageView.frame = CGRectMake(soundButton.leftImageView.right+10, 12, 20, 15);
    
    soundButton.voiceTimeLabel.frame = CGRectMake(soundButton.voiceImageView.right, -5, soundButton.width-soundButton.voiceImageView.right-15, soundButton.height);
    [soundButton setImage:[UIImage imageNamed:@"语音播放长条"] forState:0];
    [backView addSubview:(self.soundButton=soundButton)];
    self.model.cellHeight = self.backView.bottom+20;
 
}

-(void)setExtDict:(NSDictionary *)extDict{
    if ([extDict[@"SoundUrl"] length]){
        self.titleLabel.height -= 70;
        self.soundButton.soundUrl = extDict[@"SoundUrl"];
        self.soundButton.voiceTimeLabel.text = [NSString stringWithFormat:@"%@S",extDict[@"SoundLength"]];
        self.backView.height +=70;
        self.model.cellHeight = self.backView.bottom+20;
    }
}

+ (NSString *)cellIdentifier
{
    return @"HRChatAdminCell";
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
